﻿using System;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using Crossroad.Utils.Extensions;
using Crossroad.Core.Configs;
using Crossroad.OAuth.Attributes;
using Crossroad.System.Api;
using Crossroad.System.Widget.Model;
using Crossroad.System.Widget.Service;
using Crossroad.System.Widget.Service.Impl;
using Crossroad.System.Widget.Dto;
using Crossroad.System.Admin.Attributes;

namespace  Crossroad.System.Widget.Controller
{
    /// <summary>
    /// 通知公告
    /// </summary>
    [ApiAuthorize]
    [Route("api/widget/notices")]
    public class NoticeController : BaseApi
    {
        private readonly NoticeService service;

        public NoticeController(
            INoticeService serviceIpt
        )
        {
            service = serviceIpt as NoticeService;
        }

        /// <summary>
        /// 查询列表数据
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="keywords"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        [Permission("widget/notice/")]
        [HttpGet]
        public object GetList(
            int? pageIndex = null,
            int? pageSize = null,
            string keywords = ""
        )
        {
            //无分页列表查询
            if (pageIndex == null && pageSize == null)
                return service.GetList(queryable =>
                {
                    queryable
                        .OrderBy(a => a.publishDate, OrderByType.Desc)
                        .OrderBy(a => a.createTime, OrderByType.Desc);

                    if (keywords.Contains())
                        queryable.Where(a => SqlFunc.Contains(a.title, keywords));
                });

            //有分页列表查询
            pageIndex = pageIndex ?? 1;
            pageSize = pageSize ?? AppConfig.DEFAULT_PAGE_SIZE;

            return service.GetPaging(
                pageIndex.Value,
                pageSize.Value,
                queryable =>
                {
                    queryable
                        .OrderBy(a => a.publishDate, OrderByType.Desc)
                        .OrderBy(a => a.createTime, OrderByType.Desc);

                    if (keywords.Contains())
                        queryable.Where(a => SqlFunc.Contains(a.title, keywords));
                }
            );
        }

        /// <summary>
        /// 查询详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public object GetDetail(
            int id
        )
        {
            return service.GetEntity(id);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        [HttpPost]
        public object CreateAt(
            [FromBody] NoticeDto parameter
        )
        {
            var model = new Notice
            {
                creatorId = CurrentUser.id,
                publishDate = parameter.publishDateText.Replace("-", "").ToInt(),
        };
            parameter.CopyTo(model);

            return service.CreateAt(model);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="parameter"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPatch("{id}")]
        public object UpdateAt(
            [FromBody] NoticeDto parameter,
            int id
        )
        {
            parameter.id = id;
            parameter.publishDate = parameter.publishDateText.Replace("-", "").ToInt();

            return service.UpdateAt(parameter);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        [HttpDelete("{id}")]
        public object DeleteAt(int id)
        {
            service.DeleteAt(new NoticeDto
            {
                id = id
            });

            return true;
        }

    }
}